﻿using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.Service.Model.InterProxy;
using Ewell.BizComponent.Service.Model.Invoice.Request;
using Ewell.BizComponent.Service.Model.Invoice.Response;
using Ewell.BizComponent.Service.Model.Request;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Ewell.BizComponent.Service.Server
{
    /// <summary>
    /// 发票
    /// </summary>
    public class InvoiceServer : IInvoiceServer
    {
        const string interproxyProName = "bin\\plugin\\Ewell.Interface.Proxy.exe";
        /// <summary>
        /// 获取可用票据
        /// </summary>
        List<CurrentInvoiceResponse> IInvoiceServer.QueryCurrentInvoice(long interfaceId, CurrentInvoiceRequest request)
        {
            //接口调用参数
            RequestHeader requestHeader = new RequestHeader()
            {
                interfaceId = interfaceId,
                tradeCode = "INV001",
                employeeNo = LoginInfo.loginUser.no,
                employeeName = LoginInfo.loginUser.userName,
                deptNo = LoginInfo.loginUser.deptCode,
                deptName = LoginInfo.loginUser.deptName,
                zone = LoginInfo.loginUser.zone,
            };

            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(requestHeader),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            List<CurrentInvoiceResponse> result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<List<CurrentInvoiceResponse>>();
            else if (clientResult.@object?.GetType() == typeof(JArray))
                result = ((JArray)clientResult.@object).ToObject<List<CurrentInvoiceResponse>>();
            else
                result = (List<CurrentInvoiceResponse>)clientResult?.@object;
            return result;
        }
        /// <summary>
        /// 开具票据
        /// </summary>
        InvoicingResponse IInvoiceServer.IssueInvoice(long interfaceId, InvoiceIssueRequest request)
        {
            //接口调用参数
            RequestHeader requestHeader = new RequestHeader()
            {
                interfaceId = interfaceId,
                tradeCode = "INV101",
                employeeNo = LoginInfo.loginUser.no,
                employeeName = LoginInfo.loginUser.userName,
                deptNo = LoginInfo.loginUser.deptCode,
                deptName = LoginInfo.loginUser.deptName,
                zone = LoginInfo.loginUser.zone,
            };

            ProxyRequest proxyRequest = new ProxyRequest()
            {
                authentication = JsonConvert.SerializeObject(LoginInfo.loginUser),
                header = JsonConvert.SerializeObject(requestHeader),
                domain = JsonConvert.SerializeObject(request),
            };
            InterfaceClientResult clientResult = ProcessHelper.ProcessInvoke(interproxyProName, proxyRequest);
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            InvoicingResponse result = default;
            if (clientResult.@object?.GetType() == typeof(JObject))
                result = ((JObject)clientResult.@object).ToObject<InvoicingResponse>();
            else if (clientResult.@object?.GetType() == typeof(JArray))
                result = ((JArray)clientResult.@object).ToObject<InvoicingResponse>();
            else
                result = (InvoicingResponse)clientResult?.@object;
            return result;
        }
    }
}
